package cn.tedu.jewelry.mapper;

import cn.tedu.jewelry.dto.ProductDTO;
import cn.tedu.jewelry.entity.Product;
import cn.tedu.jewelry.vo.ProductBrandCategoryVO;
import cn.tedu.jewelry.vo.ProductStandVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

//商品模块
@Mapper
public interface ProductMapper {

    //商品列表页
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Select("select * from product")
    List<Product> getProductList();

    //商品详情页(用户通过某个商品的id 查询这个商品的详细信息)
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Select("select * from product where id=#{id}")
    ProductStandVO getProductById(Long id);

    //用户根据标题搜索结果页(根据标题获取所有相关的商品)  (标题==名称)
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Select("select * from product where title like concat('%',#{title},'%') and enable=1")
    List<Product> searchResult(String title);

    //管理员根据标题搜索结果页(根据标题获取所有相关的商品)  (标题title==名称name)
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Select("select * from product where title=#{title}")
    List<Product>  getProductByName(String title);

    //管理员通过商品编号查询商品
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Select("select * from product where code=#{code}")
    List<Product> getProductByCode(Integer code);

    //添加商品
    @Result(column = "category_id",property = "categoryId")
    @Result(column = "brand_id",property = "brandId")
    @Result(column = "short_title",property = "shortTitle")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "sale_count",property = "saleCount")
    @Result(column = "gmt_created",property = "gmtCreated")
    @Insert("insert into product values(null,#{categoryId},#{brandId},#{code},#{title},#{shortTitle},#{content},#{url},#{viewCount},#{saleCount},0,#{gmtCreated},#{tags},#{enable})")
    void insert(ProductDTO productDTO);

    //获取商品+品牌信息+分类信息
    @Select("select p.id,p.code,p.title,p.short_title shortTitle,p.view_count viewCount,p.sale_count saleCount,p.stock,p.gmt_created gmtCreated,p.tags,p.`enable` productEnable,c.category_name categoryName,b.brand_name brandName from product p join brand b join category c on p.brand_id=b.id and p.category_id=c.id")
    List<ProductBrandCategoryVO> getProductAndBrandAndCategory();

    //修改商品启用状态
    @Update("update product set enable=#{enable} where id=#{id}")
    void updateProductByIdAndEnable(Long id,Integer enable);

    //删除商品
    @Delete("delete from product where id=#{id}")
    void deleteById(Long id);
}
